/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.proescola.dao;

import br.com.proescola.modelo.Aluno;
import br.com.proescola.modelo.Professor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Joao
 */
public class ProfessorDAO {

    private Connection conexao;
    //private PreparedStatement stmt;

    public ProfessorDAO() throws SQLException {
        this.conexao = CriaConexao.getConexao(); //Istancia um objeto Connection, que é o parâmetro da classe CriaConexao.java
    }

    public void cadastrar(Professor p1) throws SQLException {
        //Prepara a estrutura da Query e declara o objeto PreparedStatement
        String sql = "INSERT INTO professor (nm_professor, rg_professor, cpf_professor, endereco_professor, telefone_professor, celular_professor, ic_coordenador_sim_nao) VALUES (?,?,?,?,?,?,?)";
        PreparedStatement stmt = conexao.prepareStatement(sql);
        String ret = "erro";
        //Seta os valores que vão ser inseridos na Query
        stmt.setString(1, p1.getNome());
        stmt.setString(2, p1.getRg());
        stmt.setString(3, p1.getCpf());
        stmt.setString(4, p1.getEndereco());
        stmt.setString(5, p1.getTelefone());
        stmt.setString(6, p1.getCelular());
        stmt.setString(7, p1.getCoordenador());
        //Executa a inserção SQL
        stmt.execute();
        stmt.close();

    }

    public List<Professor> getLista() throws SQLException {
        String sql = "SELECT * FROM professor";
        PreparedStatement stmt = this.conexao.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();

        List<Professor> professores = new ArrayList<Professor>();
        while (rs.next()) {
            Professor p1 = new Professor(rs.getInt("cd_professor"), rs.getString("nm_professor"), rs.getString("rg_professor"), rs.getString("cpf_professor"), rs.getString("endereco_professor"), rs.getString("telefone_professor"), rs.getString("celular_professor"), rs.getString("ic_coordenador_sim_nao"));
            professores.add(p1);
        }
        rs.close();
        stmt.close();
        return professores;
    }
    public List<Professor> getById(String x) throws SQLException {
        String sql = "SELECT * FROM professor WHERE nm_professor LIKE ?";
        PreparedStatement stmt = this.conexao.prepareStatement(sql);
        stmt.setString(1, ("%" + x + "%"));
        ResultSet rs = stmt.executeQuery();

        List<Professor> professores = new ArrayList<Professor>();
        while (rs.next()) {
            Professor p1 = new Professor(rs.getInt("cd_professor"), rs.getString("nm_professor"), rs.getString("rg_professor"), rs.getString("cpf_professor"), rs.getString("endereco_professor"), rs.getString("telefone_professor"), rs.getString("celular_professor"), rs.getString("ic_coordenador_sim_nao"));
            professores.add(p1);
        }
        rs.close();
        stmt.close();
        return professores;
    }
}
